1
|
|
|
/*eslint no-unused-vars: 0*/ |
2
|
|
|
|
3
|
|
|
import { fromJS, Map } from 'immutable'; |
|
|
|
|
4
|
|
|
import { createReducer } from '../../helpers/store'; |
5
|
|
|
import { AuthActionTypes } from './actions'; |
6
|
|
|
import { |
7
|
|
|
setSession, |
8
|
|
|
setUser, |
9
|
|
|
getSession, |
10
|
|
|
getIsAuthenticated, |
11
|
|
|
getUser, |
12
|
|
|
removeSession, |
13
|
|
|
removeUser |
14
|
|
|
} from '../../helpers/auth'; |
15
|
|
|
|
16
|
|
|
const initialState = fromJS({ |
17
|
|
|
session: getSession(), |
18
|
|
|
user: getUser(), |
19
|
|
|
isAuthenticated: getIsAuthenticated() |
20
|
|
|
}); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Called when login request is initiated. |
24
|
|
|
* |
25
|
|
|
* @param {Map} state |
26
|
|
|
* @param {Object} action |
27
|
|
|
* @returns {Map} |
28
|
|
|
*/ |
29
|
|
|
export function handleLoginRequest(state, action) { |
|
|
|
|
30
|
|
|
return state.set('session', fromJS({ isLoading: true })); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Called when login request is successful. |
35
|
|
|
* |
36
|
|
|
* @param {Map} state |
37
|
|
|
* @param {Object} action |
38
|
|
|
* @returns {Map} |
39
|
|
|
*/ |
40
|
|
|
export function handleLoginSuccess(state, action) { |
41
|
|
|
if (action.response.status !== 200) { |
42
|
|
|
return handleLoginFailure(state, { error: action.data.message }); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
setSession(action.data); |
46
|
|
|
|
47
|
|
|
return state |
48
|
|
|
.set('session', fromJS(action.data)) |
49
|
|
|
.set('isAuthenticated', true); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Called when the login request fails. |
54
|
|
|
* |
55
|
|
|
* @param {Map} state |
56
|
|
|
* @param {Object} action |
57
|
|
|
* @returns {Map} |
58
|
|
|
*/ |
59
|
|
|
export function handleLoginFailure(state, action) { |
60
|
|
|
return state.set('session', fromJS({ errorMessage: action.error })); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Called when the user is logged out. |
65
|
|
|
* |
66
|
|
|
* @param {Map} state |
67
|
|
|
* @param {Object} action |
68
|
|
|
* @returns {Map} |
69
|
|
|
*/ |
70
|
|
|
export function handleLogout(state, action) { |
|
|
|
|
71
|
|
|
removeSession(); |
72
|
|
|
removeUser(); |
73
|
|
|
|
74
|
|
|
return state |
75
|
|
|
.delete('session') |
76
|
|
|
.delete('user') |
77
|
|
|
.set('isAuthenticated', false); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Called when a load request is initiated. |
82
|
|
|
* |
83
|
|
|
* @param {Map} state |
84
|
|
|
* @param {Object} action |
85
|
|
|
* @returns {Map} |
86
|
|
|
*/ |
87
|
|
|
export function handleUserLoadRequest(state, action) { |
|
|
|
|
88
|
|
|
return state.set('user', fromJS({ isLoading: true })); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Called when the load user is successful. |
93
|
|
|
* |
94
|
|
|
* @param {Map} state |
95
|
|
|
* @param {Object} action |
96
|
|
|
* @returns {Map} |
97
|
|
|
*/ |
98
|
|
|
export function handleUserLoadSuccess(state, action) { |
99
|
|
|
if (action.response.status !== 200) { |
100
|
|
|
return handleUserLoadFailure(action, { error: action.data.message }); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
setUser(action.data); |
104
|
|
|
|
105
|
|
|
return state.set('user', fromJS(action.data)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Called when a load request fails. |
110
|
|
|
* |
111
|
|
|
* @param {Map} state |
112
|
|
|
* @param {Object} action |
113
|
|
|
* @returns {Map} |
114
|
|
|
*/ |
115
|
|
|
export function handleUserLoadFailure(state, action) { |
116
|
|
|
return state.set('user', fromJS({ errorMessage: action.error })); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
export const authReducer = createReducer(initialState, { |
120
|
|
|
[AuthActionTypes.LOGIN_REQUEST]: handleLoginRequest, |
121
|
|
|
[AuthActionTypes.LOGIN_SUCCESS]: handleLoginSuccess, |
122
|
|
|
[AuthActionTypes.LOGIN_FAILURE]: handleLoginFailure, |
123
|
|
|
[AuthActionTypes.LOGOUT]: handleLogout, |
124
|
|
|
[AuthActionTypes.USER_LOAD_REQUEST]: handleUserLoadRequest, |
125
|
|
|
[AuthActionTypes.USER_LOAD_SUCCESS]: handleUserLoadSuccess, |
126
|
|
|
[AuthActionTypes.USER_LOAD_FAILURE]: handleUserLoadFailure |
127
|
|
|
}); |
128
|
|
|
|